import router from '@/router';
import axios from 'axios';
// import Cookie from 'js-cookie';
import Cookies from 'js-cookie';
const ins = axios.create({
    baseURL: 'http://localhost:8989',
    // baseURL: 'http://47.94.7.215:8989',

    // timeout:5000,
    headers: {
        'token': Cookies.get('token')
    },
    transformRequest: [function (data, headers) {
        headers['token'] = Cookies.get('token')
        return data
    }]
})

// //请求拦截
axios.interceptors.request.use(config => {
    //判断token
    let token = Cookies.get('token')
    console.log(token);
    if (token) {
        // config.headers.Authorization = token
        config.headers['token'] = token
    }

    return config
})
//响应拦截
axios.interceptors.response.use(
    response => {
        console.log(response);
        if (response.data.code !== 0) {
            alert(response)
            return Promise.resolve(response)
            // router.push("/")
        } else {
            return Promise.reject(response)
        }

    },
    error => {
        alert(error)
        console.log(error);
        return Promise.reject(error.response)
    }
);
export default ins